const initialSate = {
  // 2. 从缓存中获取初始值
  list: JSON.parse(localStorage.getItem('redux-todo-list') || '[]'),
  type: 'completed',
};

export default function todoReducer(state = initialSate, { type, payload }) {
  console.warn(' Logger ----->  ', type, payload);
  switch (type) {
    case 'delById':
      return {
        ...state,
        list: state.list.filter((item) => item.id !== payload),
      };

    case 'updateById':
      return {
        ...state,
        list: state.list.map((item) => ({
          ...item,
          isDone: item.id === payload ? !item.isDone : item.isDone,
        })),
      };

    case 'add':
      return { ...state, list: [payload, ...state.list] };
    case 'updateType':
      return { ...state, type: payload };

    case 'checkAll':
      return {
        ...state,
        list: state.list.map((item) => ({ ...item, isDone: payload })),
      };
    default:
      return state;
  }
}
